<template>
  <div class="page-search">
    <cky-form v-bind="props.searchFormConfig" v-model="formData">
      <template #header>
        <h1 class="header">搜索用户</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button icon="el-icon-refresh" @click="handleResetClick"
            >重置</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleSearchClick"
            >搜索</el-button
          >
        </div>
      </template>
    </cky-form>
  </div>
</template>

<script lang="ts" setup>
import { defineProps, defineEmits, ref } from 'vue'
import CkyForm from '@/base-ui/form'
const props = defineProps({
  searchFormConfig: {
    type: Object,
    required: true
  }
})
const emits = defineEmits(['resetBtnClick', 'searchBtnClick'])

// 根据配置中的field来双向绑定属性
const formItems = props.searchFormConfig?.formItems ?? []
const formOriginData: any = {}
for (const item of formItems) {
  formOriginData[item.field] = ''
}
const formData = ref(formOriginData)

const handleResetClick = () => {
  // for (const key in formOriginData) {
  //   formData.value[key] = formOriginData[key]
  // }
  formData.value = formOriginData
  emits('resetBtnClick')
}
const handleSearchClick = () => {
  emits('searchBtnClick', formData.value)
}
</script>

<style scoped lang="less">
.header {
  color: #409eff;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
